package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealDtoMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.pojo.SetmealDto;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDtoMapper setmealDtoMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional//开启事务
    public R addSetmeal(SetmealDto setmealDto) {
        setmealMapper.insert(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDishMapper.insert(setmealDish);
        }
        return R.success("添加成功");
    }

    @Override
    public R page(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> page1 = new Page<>(page,pageSize);
        LambdaQueryWrapper<SetmealDto> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name!=null,SetmealDto::getName,name);
        Page<SetmealDto> setmealDtoPage = setmealDtoMapper.selectPage(page1, wrapper);
        List<SetmealDto> records = setmealDtoPage.getRecords();
        for (SetmealDto record : records) {
            String name1 = categoryMapper.selectById(record.getCategoryId()).getName();
            record.setCategoryName(name1);
        }
        return R.success(setmealDtoPage);
    }

    @Override
    public R deleteByIds(Long[] ids) {
        setmealMapper.deleteBatchIds(Arrays.asList(ids));
        for (Long id : ids) {
            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SetmealDish::getSetmealId,id);
            setmealDishMapper.delete(wrapper);
        }
        return R.success("删除成功");
    }

    @Override
    public R upDataSt(Integer id, Long[] ids) {
        LambdaUpdateWrapper<Setmeal> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Setmeal::getStatus,id).in(Setmeal::getId,ids);
        setmealMapper.update(null,wrapper);
        return R.success("成功");
    }

    @Override
    public R findById(Long id) {
        SetmealDto setmealDto = setmealDtoMapper.selectById(id);
        LambdaQueryWrapper<Category> wrapper  = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getId,setmealDto.getCategoryId());
        Category category = categoryMapper.selectOne(wrapper);
        setmealDto.setCategoryName(category.getName());
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper1);
        setmealDto.setSetmealDishes(setmealDishes);
        return R.success(setmealDto);
    }

    @Override
    @Transactional
    public R upDataSetmeal(SetmealDto setmealDto) {
        setmealDtoMapper.updateById(setmealDto);
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishMapper.delete(wrapper);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDishMapper.insert(setmealDish);
        }
        return R.success("修改成功");
    }

    @Override
    public R list(Long categoryId, Integer statues) {
        System.out.println(statues);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId,categoryId);
        List<Setmeal> setmeals = setmealMapper.selectList(wrapper);
        return R.success(setmeals);
    }
}
